<!--  -->
<template>
  <div class="page">
    <van-popup v-model="hotelListFlag" round closeable get-container="#app">
      <div class="hotel_list_box">
        <div class="popup_title">酒店列表</div>
        <van-search
          class="margin10"
          v-model="hotelListQuery.keyword"
          placeholder="请输入酒店或省市区"
          @input="getHotelList"
        />
        <div class="hotel_list">
          <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
              <van-cell v-for="(item,index) in list" :key="index" @click="itemClick(item)">
                <p class="list_p">{{item.green_hotel_name}}</p>
                <p class="list_p">{{item.address_p}}{{item.address_c}}{{item.address_a}}</p>
              </van-cell>
            </van-list>
          </van-pull-refresh>
        </div>
      </div>
    </van-popup>
    <van-nav-bar class="nav_bar_class" title="加入酒店" left-arrow @click-left="goBack" />
    <div class="content">
      <div class="content_box">
        <div class="content_title">请填写贵酒店的编码并加入酒店，如果查询不到编码请登记酒店</div>
        <div class="content_title_main">
          <div class="main_title">您的酒店编码</div>
          <div class="main_input">
            <van-field style="background:none" v-model="value" placeholder="请输入您的酒店编码" />
          </div>
          <div class="main_tips">
            <p>1.酒店编码是以6位由数字和字母组成的唯一编码</p>
            <p>2.您可以在此点击查询贵酒店的酒店编码</p>
          </div>
          <div class="margin10">
            <van-button class="btn_color" round type="info" @click="hotelListFlag = true">查询编码</van-button>
          </div>
          <div class="margin20">
            <van-button class="btn_color" type="primary" @click="submit" block>提交</van-button>
            <van-button class="btn_color_default" type="default" @click="goRegisterHotel" block>登记酒店</van-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import { apiSearchHotelInfo, apiAddToHotel } from "@/config/api";
export default {
  data() {
    return {
      hotelListQuery: {
        address_p: "",
        address_c: "",
        address_a: "",
        keyword: ""
      },
      value: "",
      hotelListFlag: false,
      list: [],
      loading: false,
      finished: false,
      refreshing: false
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    getHotelList() {
      if (this.refreshing) {
        this.list = [];
        this.refreshing = false;
      }
      apiSearchHotelInfo(this.hotelListQuery)
        .then(res => {
          if (res.code == 100) {
            // console.log(this.list);
            this.list = JSON.parse(res.data).hotelInfoList;
            this.loading = false;
            this.finished = true;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    onLoad() {
      this.getHotelList();
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
    itemClick(item) {
      this.hotelListFlag = false;
      this.value = item.serial_num;
    },
    submit() {
      let data = {
        serial_num: this.value
      };
      apiAddToHotel(data)
        .then(res => {
          if (res.code == 100) {
            Toast.success("提交成功,请等待审核");
            this.$router.push("/login");
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    goRegisterHotel() {
      this.$router.push("/hotel_register_join");
    }
  }
};
</script>

<style scoped>
.nav_bar_class {
  background: #b0c870;
}
.page >>> [class*="van-hairline"]::after {
  border: none;
}
.page >>> .van-nav-bar__title {
  color: #45862c;
}
.page >>> .van-nav-bar .van-icon {
  color: #45862c;
}
.content {
  width: 100%;
  box-sizing: border-box;
  padding: 0px 20px;
}
.content_box {
  width: 100%;
  height: 472px;
  background: #bcd38b;
  border-radius: 10px;
  margin-top: 10px;
  overflow: hidden;
}
.content_title {
  width: 100%;
  font-size: 13px;
  box-sizing: border-box;
  padding: 10px;
  background: #45862c;
  color: #fff;
}
.content_title_main {
  width: 100%;
  box-sizing: border-box;
  padding: 5px 10px;
}
.main_title {
  color: #45862c;
  font-size: 14px;
  font-weight: bold;
}
.main_input {
  height: 45px;
  width: 100%;
  border-radius: 5px;
  overflow: hidden;
  border: solid 1px #45862c;
  margin-top: 10px;
}
.main_tips {
  margin-top: 10px;
  line-height: 25px;
  color: #333;
}
.main_btn {
  margin-top: 10px;
}
.btn_color {
  background: #418732;
  color: #fff;
  border: none;
}
.btn_color_default {
  background: none;
  color: #418732;
  border: solid 1px #418732;
  margin-top: 10px;
}
.hotel_list_box {
  width: 300px;
  height: 500px;
  background: #fff;
}
.popup_title {
  width: 100%;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 12px;
  font-size: 14px;
}
.list_p {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
}
.hotel_list {
  height: 400px;
  overflow-y: auto;
}
.list_p:nth-child(2) {
  color: #666;
}
</style>
